<template>
	<view>
		<view class="con title">
			<text>发布新鲜事</text>
			<text @click="send">发布</text>
		</view>
		<view class="con"><textarea maxlength="-1" placeholder="输入内容..." v-model="title" /></view>
		<view class="con">
			<robby-image-upload :value="imageData" :limit="limitNumber" fileKeyName="file" @delete="deleteImage" @add="addImage" :server-url="serverUrl"></robby-image-upload>
		</view>
		<uni-popup ref="sendError" type="message"><uni-popup-message type="error" message="发布失败" :duration="1000"></uni-popup-message></uni-popup>
		<uni-popup ref="sendSuccess" type="message"><uni-popup-message type="success" message="发布成功" :duration="1000"></uni-popup-message></uni-popup>
	</view>
</template>

<script>
import robbyImageUpload from '@/components/robby-image-upload/robby-image-upload.vue';
export default {
	data() {
		return {
			limitNumber: 9,
			imageData: [],
			serverUrl: 'http://172.17.26.211:3334/uploadFile',
			title: '',
			wx_user_id: 0
		};
	},
	created() {
		uni.getStorage({
			key: 'myInfo',
			success: res => {
				console.log(res.data.id);
				this.wx_user_id = res.data.id;
			}
		});
	},
	components: { robbyImageUpload },
	methods: {
		deleteImage(e) {
			this.imageData.splice(e.allImages.length, 1);
		},
		addImage(e) {
			const { data } = JSON.parse(e.allImages[e.allImages.length - 1]);
			this.imageData.push(data);
			console.log(this.imageData[0]);
		},
		//时间转换
		format(value) {
			let date = new Date(value);
			let y = date.getFullYear();
			let m = date.getMonth() + 1;
			let d = date.getDate();
			let hour = date.getHours();
			let minutes = date.getMinutes();
			let second = date.getSeconds();
			return y + '-' + m + '-' + d + ' ' + (hour < 10 ? '0' + hour : hour) + ':' + (minutes < 10 ? '0' + minutes : minutes) + ':' + (second < 10 ? '0' + second : second);
		},
		async send() {
			const newDate = this.format(Date.parse(new Date()));
			const { data: res } = await this.$http({
				url: '/newthing/addNewThing',
				data: {
					imgs_url: this.imageData[0],
					title: this.title,
					wx_user_id: this.wx_user_id,
					create_time: newDate
				}
			});
			if (res.code != 200) this.$refs.sendError.open();
			this.$refs.sendSuccess.open();
			uni.reLaunch({
				url: '/pages/new/new'
			});
		}
	}
};
</script>

<style lang="less">
@import url('@/static/font/iconfont.css');
.con {
	width: 690rpx;
	margin: 0 auto;
}
.title {
	display: flex;
	justify-content: space-between;
	align-items: center;
	height: 110rpx;
	> text:first-child {
		height: 64rpx;
		font-size: 43rpx;
		font-weight: bold;
	}
	> text:last-child {
		width: 130rpx;
		height: 64rpx;
		background-color: #f9cc66;
		font-size: 32rpx;
		font-weight: bold;
		display: flex;
		justify-content: center;
		align-items: center;
		border-radius: 30rpx;
	}
}
textarea {
	font-size: 30rpx;
	width: 690rpx;
}
</style>
